package com.testcase.visualization;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;

import com.testcase.R;
import com.testcase.SensorNames;

public class VisualInterfaceActivity extends Activity {

	//Values Set by Android for visibility of WebView
	private static final int GONE = 8;
	private static final int VISIBLE = 0;
	
	private List<String> senList;
	//private List<List<String>> dataList;
	private int id;
	private WebView wv;
	private SensorNames bothTitles;
	private ProgressBar pb;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Request for Progress Bar
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.graphwebchoose);
		pb = (ProgressBar) findViewById(R.id.progressBar);
		
		//Grab Intents
		@SuppressWarnings("unchecked")
		HashMap<String, List<String>> sensor = (HashMap<String, List<String>>) getIntent().getSerializableExtra("sensorList");
		senList = sensor.get("senList");
		id = (int) getIntent().getIntExtra("index", -1);
		bothTitles = new SensorNames(senList.get(id));
		
		//Set up Spinner
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.chartList, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Spinner spin = (Spinner) findViewById(R.id.graphSpinner);
		spin.setAdapter(adapter);		
		
		//Set Up First Visualization
		wv = (WebView) findViewById(R.id.wv1);
		wv.getSettings().setJavaScriptEnabled(true);
		final GetSensorInfo getSensorInfo = new GetSensorInfo();
		//JavascriptInterface allows the use of Java Functions in HTML
		wv.addJavascriptInterface(getSensorInfo, "android");
		wv.setWebChromeClient(new WebChromeClient());
		//Set WebView invisible so the progress bar can display instead.
		wv.setVisibility(GONE);
		
		setProgressBarVisibility(true);
		setProgressBarIndeterminateVisibility(true);
		//Once the page has loaded make WebView visible and progress invisible
		wv.setWebViewClient(new WebViewClient() {
		
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				pb.setVisibility(GONE);
				setProgressBarIndeterminateVisibility(false);
				wv.clearView();
				wv.setVisibility(VISIBLE);
			}
		});
		
		//Spinner Listener to choose different graphs
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position) {
				//Area Chart
				case 0:
					wv.setVisibility(GONE);
					pb.setVisibility(VISIBLE);
					setProgressBarIndeterminateVisibility(true);
					wv.loadUrl("file:///android_asset/visual/ImageAreaChart.html");
					//wv.reload();
					break;
				//Line Chart
				case 1:
					wv.setVisibility(GONE);
					pb.setVisibility(VISIBLE);
					setProgressBarIndeterminateVisibility(true);
					wv.loadUrl("file:///android_asset/visual/ImageLineChart.html");
					//wv.reload();
					break;
				//Bar Chart
				case 2:
					wv.setVisibility(GONE);
					pb.setVisibility(VISIBLE);
					setProgressBarIndeterminateVisibility(true);
					wv.loadUrl("file:///android_asset/visual/ImageBarChart.html");
					break;
				//Column Chart
				case 3:
					wv.setVisibility(GONE);
					pb.setVisibility(VISIBLE);
					setProgressBarIndeterminateVisibility(true);
					wv.loadUrl("file:///android_asset/visual/ImageColumnChart.html");
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// Do Nothing
				
			}
			
		});
		//Button to show data if clicked
		Button but = (Button) findViewById(R.id.getNewGraph);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				wv.setVisibility(GONE);
				pb.setVisibility(VISIBLE);
				setProgressBarIndeterminateVisibility(true);
				EditText edit = (EditText) findViewById(R.id.amountOfData);
				getSensorInfo.setBeforeE(edit.getText().toString());
				wv.reload();
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.resfreshmenu, menu);
		return true;
	}
	
	//Refresh menu option
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.refresh:
			wv.setVisibility(GONE);
			pb.setVisibility(VISIBLE);
			setProgressBarIndeterminateVisibility(true);
			wv.reload();
			return true;
		default:
			return true;
		}
	}
	
	//Class used by HTML Files
	final class GetSensorInfo {
		
		private String beforeE;
		
		public String sensorName() {
			return bothTitles.getTitle();
		}
		public String fullName() {
			return bothTitles.getFullTitle();
		}
		
		public String getBeforeE() {
			if(beforeE == "" || beforeE == null) {
				return "10";
			}
			return beforeE;
		}
		
		public void setBeforeE(String elements) {
			beforeE = elements;
		}
	}
}
